package com.weimob.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * @program: xh-learn-record
 * @description:
 * @author: sch
 * @create: 2022-12-20 16:57
 **/
public class A003_CustomProducerSync {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1,创建kafka生产者对象
        //1.1 创建生产者配置对象
        Properties properties = new Properties();
        //1.2 添加kafka配置信息
        //botstrap.servers 信息
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.1.3:9092,192.168.1.3:9093,192.168.1.3:9094");
        //k,v序列化
//        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
//                "org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
//        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
//                "org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());
        //1.3 创建kafka生产者对象
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);

        //2,发送消息
        for (int i = 0; i < 10; i++) {
            kafkaProducer.send(new ProducerRecord<String, String>("first", "hell xhx" + i)).get();
        }

        //3,关闭资源
        kafkaProducer.close();
    }
}
